<template>
  <div class="app-main-container">
    <div>
      <el-input
        v-model="mealName"
        :placeholder="$t('message.qsr') + $t('message.tcname')"
        class="input-with-select bottom-speace-style"
        style="width:50%"
        @keyup.enter.native="searchMeals"
      >
        <el-button slot="append" icon="el-icon-search" @click="searchMeals" />
      </el-input>
    </div>
    <div class="top-speace saleTickets">
      <div
        v-for="(item, index) in saleTickets"
        :key="index"
        class="slode-style"
      >
        <div
          class="card-style"
          :class="isSelected(index)?'select-style':'normal-style'"
          @click="selectTick(index)"
        >
          <div class="img-div">
            <img :src="item.imgurl" alt="">
          </div>
          <!-- <img :src="item.imgurl" alt=""> -->
          <!-- {{ '【'+item.projectName+'】'+item.name }} -->
          <div class="card-name">{{ mealTitle(item) }}</div>
          <div class="time-style">{{ $t('message.yxq') + '：' + item.indate + $t('message.tian') }}</div>
          <div class="card-first-price secondColor">{{ $t('message.rmbfh') }}{{ item.price }}</div>
          <div class="num-module-style">
            <div class="sale-style">
              <span class="num-title-style">{{ $t('message.yishou') }}：</span>
              <span class="secondColor">{{ item.circulationDay - item.surplusDay }}</span>
            </div>
            <div class="surplus-style">
              <span class="num-title-style">{{ $t('message.sy') }}：</span>
              <span class="secondColor">{{ item.surplusDay }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="line-style top-speace" />

    <div class="property-title top-speace">{{ $t('message.dgtc') }} </div>
    <el-table
      :data="selectedMessage"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column align="center" :label="$t('message.xh')" type="index" :width="$i18n.locale === 'ZH'? 50 : 120" />
      <el-table-column align="center" :label="$t('message.tcname')" prop="name" />
      <el-table-column align="center" :label="$t('message.lx')" prop="name">
        <template slot-scope="scope">
          <span class="count-style">{{ scope.row.mealsType == '0' ? $t('message.mpcsk') : scope.row.mealsType == '1'? $t('message.mpqxk'):$t('message.ydck') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('message.cs')" prop="nums" />
      <el-table-column align="center" :label="$t('message.zscs')">
        <template slot-scope="scope">
          <el-input-number v-if="scope.row.mealsType !== '1'" v-model="scope.row.giveNums" :disabled="!zsnum" size="small" :step="1" step-strictly :min="0" :max="9999" :label="$t('message.zscs')" style="width: 100%" />
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('message.yxq')" prop="indate" />
      <el-table-column align="center" :label="$t('message.num')">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.count" size="small" step-strictly :step="1" :min="1" :label="$t('message.num')" style="width: 100%" />
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('message.price')" prop="price" />
      <el-table-column align="center" :label="$t('message.caozuo')">
        <template slot-scope="scope">
          <el-button v-preventReClick type="text" class="secondColor" @click="toDelete(scope.$index)">{{ $t('message.del') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div v-if="selectedMessage.length" class="bottom-style">
      <div class="sum-style">
        <span class="sum-title">{{ $t('message.heji') }} ：</span>
        <span class="sum-value secondColor">{{ totalMoney }}</span>
      </div>
      <div class="btn-style" @click="totalAction">{{ $t('message.qjs') }}</div>
    </div>

    <orderDialog ref="child" @orderSuccess="getList" />
    <comboConfirm ref="comboConfirm" @confirm="createOrder" />
  </div>
</template>

<script>
import { mealList, configList } from '@/api'
import { parseTime } from '@/utils'
import orderDialog from '@/components/order'
import comboConfirm from '@/components/ComboConfirm'
export default {
  name: 'ComboSell',
  components: {
    orderDialog,
    comboConfirm
  },
  data() {
    return {
      swiperOptions: {
        slidesPerView: 'auto',
        pagination: {
          el: '.swiper-pagination'
        }
      },
      mealName: '',
      cardNum: '',
      dialogMessageTableVisible: false,
      dialogTableVisible: false,
      showMessage: false,
      cardMessage: {},
      date: null,
      zsnum: false,
      saleTickets: [],
      selectedTickets: [],
      selectedMessage: [],
      project: {
        projectList: []
      },
      param: {
        date: parseTime(new Date())
      }
    }
  },
  computed: {
    // swiper() {
    //   return this.$refs.mySwiper.$swiper
    // },
    totalMoney() {
      let money = 0.0
      this.selectedMessage.forEach(item => {
        money += item.price * item.count
      })
      return parseFloat(money).toFixed(2)
    }
  },
  created() {
    this.getMeals()
    this.getDefalutConfig()
  },
  methods: {
    getDefalutConfig() {
      configList({ param: { type: '6' }})
        .then((res) => {
          if (res.errorCode !== '000000') return
          const list = res.recordList
          const objb = list.find(item => item.code === 'PRESENT_NUM_CONFIG')
          if (objb && objb.name) {
            this.zsnum = true
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
    getList() {
      this.$router.push({ name: 'ComboOrderList' })
    },
    mealTitle(item) {
      let titleStr = ''
      if (item.mealsType === '0') {
        titleStr = '【' + this.$t('message.mpcsk') + '】' + item.name
      } else if (item.mealsType === '1') {
        titleStr = '【' + this.$t('message.mpqxk') + '】' + item.name
      } else {
        titleStr = '【' + this.$t('message.ydck') + '】' + item.name
      }
      return titleStr.length > 30 ? titleStr.slice(0, 27) + '...' : titleStr
    },
    searchMeals() {
      this.getMeals()
    },
    mealType(item) {
      let typeStr = ''
      if (item.mealsType === '0') {
        typeStr = this.$t('message.cs') + '：' + item.nums
      } else {
        typeStr = this.$t('message.yxq') + '：' + item.indate
      }
      return typeStr
    },
    getMeals() {
      mealList({
        param: {
          name: this.mealName,
          venueId: sessionStorage.getItem('venueId')
        }
      })
        .then(res => {
          if (res.errorCode !== '000000') return
          this.saleTickets = res.recordList
          this.selectedTickets = []
          this.selectedMessage = []
        })
        .catch(err => {
          console.log(err)
        })
    },
    selectTick(index) {
      if (this.selectedTickets.includes(index)) {
        var i = this.selectedTickets.indexOf(index)
        this.selectedTickets.splice(i, 1)
        this.selectedMessage.splice(i, 1)
      } else {
        const item = this.saleTickets[index]
        this.selectedTickets.push(index)
        this.selectedMessage.push({
          id: item.id,
          mealsType: item.mealsType,
          nums: item.nums,
          giveNums: item.giveNums || 0,
          indate: item.indate,
          name: item.name,
          price: item.price,
          count: 1,
          surplusDay: item.surplusDay
        })
      }
    },
    isSelected(index) {
      return this.selectedTickets.includes(index)
    },
    toDelete(index) {
      this.selectedTickets.splice(index, 1)
      this.selectedMessage.splice(index, 1)
    },
    totalAction() {
      this.$refs.comboConfirm.open(this.selectedMessage, this.totalMoney)
    },
    createOrder(data) {
      this.$refs.child.continuePay(data.p, 'combo', data.cardMessage)
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/styles/variables.scss';
.top-speace {
  margin-top: 20px;
}
.saleTickets{
  display: flex;
  width: 100%;
  overflow: auto;
}
.slode-style {
  width: 185px;
  height: 206px;
  margin-right: 20px;
  margin-bottom: 10px;
  cursor: pointer;
}
.card-style {
  display: inline-block;
  vertical-align: middle;
  width: 185px;
  height: 205px;
  margin-right: 20px;
  // margin-bottom: 10px;
  .img-div {
    width: 165px;
    height: 75px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 10px;
    margin-left: 10px;
    img {
      height: 100%;
      margin: auto;
      border-radius: 4px;
    }
  }
  .card-name {
    color: #222;
    font-size: 13px;
    // font-weight: 500;
    margin-top: 15px;
    margin-left: 10px;
    margin-right: 10px;
    padding: 0;
    line-height: 14px;
  }
  .time-style {
    // background-color: #f00;
    color: #999;
    font-weight: 500;
    margin-top: 5px;
    margin-left: 10px;
    margin-right: 10px;
    text-align: center;
    font-size: 15px;
    line-height: 17px;
  }
  .card-first-price {
    margin-top: 5px;
    margin-left: 10px;
    margin-right: 10px;
    font-size: 15px;
    line-height: 17px;
    text-align: center;
  }
  .num-module-style {
    margin-top: 5px;
    margin-left: 10px;
    margin-right: 10px;
    font-size: 15px;
    line-height: 17px;
    .sale-style {
      float: left;
    }
    .surplus-style {
      float: right;
    }
    .num-title-style {
      color: #999;
    }
  }
}
.select-style {
  border: solid 3px $primaryColor;
}
.normal-style {
  border: solid 1px #d8d8d8;
}

.bottom-style {
  height: 50px;
  border-left: 1px #DFE2E8 solid;
  border-right: 1px #DFE2E8 solid;
  border-bottom: 1px #DFE2E8 solid;
  .sum-style {
    margin-left: 10px;
    margin-top: 17px;
    height: 15px;
    float: left;
    .sum-title {
      color: #222;
      font-size: 13px;
    }
    .sum-value {
      font-size: 17px;
      font-weight: 700;
    }
  }
  .btn-style {
    float: right;
    width: 80px;
    height: 30px;
    margin-right: 10px;
    margin-top: 10px;
    background-color: $primaryColor;
    color: #fff;
    text-align: center;
    border-radius: 4px;
    line-height: 30px;
    font-size: 13px;
    font-weight: 700;
  }
}
.count-style {
  margin-left: 5px;
  margin-right: 5px;
}
.bottom-speace-style {
  margin-bottom: 10px;
}
</style>
